﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace School
{
    public class Global : System.Web.HttpApplication
    {
        TechnologySchoolEntities db = new TechnologySchoolEntities();
        WebInfo item = null;

        protected void Application_Start(object sender, EventArgs e)
        {
            long count = 0;
            item = db.WebInfo.FirstOrDefault();
            if (item != null)
            {
                count = item.total.Value;
            }
            Application["total"] = count;
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            //Session.Timeout = 1;
            Application.Lock();
            Application["total"] = (long)Application["total"] + 1;
            Application.UnLock();
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
            if (item == null)
            {
                item = new WebInfo();
                item.total = (long)Application["total"];
                db.AddToWebInfo(item);
            }
            else
            {
                item.total = (long)Application["total"];
            }
            db.SaveChanges();
        }
    }
}